#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
using PIP = pair<int, pair<int, int>>;
const int N = 1e5 + 5, M = 2e5 + 5;

int ufs[N];
int find(int x)
{
    if(ufs[x] < 0) return x;
    return ufs[x] = find(ufs[x]);
}

bool merge(int a, int b)
{
    if(a == b) return false;
    int p1 = find(a), p2 = find(b);
    if(p1 == p2 && p1 != -1) return false;
    ufs[p1] += ufs[p2];
    ufs[p2] = p1;
    return true;
}

int main()
{
    memset(ufs, -1, sizeof ufs);
    int n, m; cin >> n >> m;
    priority_queue<PIP, vector<PIP>, greater<PIP>> pq;
    while(m--)
    {
        int a, b, c; cin >> a >> b >> c;
        pq.push({c, {a, b}});
    }
    int ans = 0, cnt = 1;
    while(pq.size())
    {
        auto cur = pq.top(); pq.pop();
        int w = cur.first, a = cur.second.first, b = cur.second.second;
        if(merge(a, b))
        {
            ans += w;
            if(++cnt == n)
            {
                cout << ans;
                return 0;
            }
        }
    }
    cout << "impossible";
    return 0;
}